﻿#-------------------------------------------------------------------------------
# Name:        dbclass.py
# Purpose:  ORM数据类型，使用storm框架实现
#
# Author:      seewind
#
# Created:     20-02-2008
# Copyright:   (c) seewind 2008
# Licence:     <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import storm.locals as storm
#import md5
import sha


class DBBaseObject(storm.Storm):
    def mystore(self):
        return storm.Store.of(self)


class DBUrl(DBBaseObject):
    """ url信息 """
    __storm_table__ = 'urlinfo'
    #id = storm.Int(primary=True)
    urlhash = storm.RawStr(primary=True)
    url = storm.RawStr()
    urlcost = storm.Int()
    createdate = storm.Date() #创建日期
    downtimes = storm.Int()  #下载次数
    size = storm.Int()
    
    @staticmethod
    def sql_createtable():
        """ 创建表的sql语句 """
        return """
        CREATE TABLE `urlinfo` (
  `urlhash` char(20) NOT NULL,
  `url` varchar(255) default NULL,
  `urlcost` int(11) default NULL,
  `createdate` date default NULL,
  `downtimes` int(11) default NULL,
  `size` int(11) default NULL,
  PRIMARY KEY  (`urlhash`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
        """
        
    def __storm_pre_flush__(self):
        """ 保存钩子(hook)：产生urlhash值 """
        if not self.urlhash:
            self.urlhash = sha.new(self.url).digest()

class DBPeer(DBBaseObject):
    """ 客户端信息 """
    __storm_table__ = 'peerinfo'
    #id = storm.Int(primary=True)
    peerid = storm.RawStr(primary=True)
    active = storm.Bool()
    ipv4 = storm.RawStr()
    cachesize = storm.Int()
    peercost = storm.Int()
    onlinetime = storm.DateTime() #在线累计时间
    pieces = storm.ReferenceSet(peerid, 'DBPiece.peerid')
    
    @staticmethod
    def sql_createtable():
        """ 创建表的sql语句 """
        return """
        CREATE TABLE `peerinfo` (
  `peerid` char(20) NOT NULL,
  `active` tinyint(1) default 0,
  `ipv4` char(12) default NULL,
  `cachesize` int(11) default NULL,
  `peercost` int(11) default NULL,
  `onlinetime` datetime default NULL,
  PRIMARY KEY  (`peerid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
        """

    def __init__(self):
        self.starttime = 0
        
    def deleteallpieces(self):
        """ 删除所有关联的piece纪录 """
        store = self.mystore()
        if store:
            self.pieces.clear()  #清除关联
            delexpr = storm.Delete(DBPiece.peerid=='', DBPiece)
            store.execute(delexpr)
    
class DBPiece(DBBaseObject):
    """ 分片信息:动态的信息 """
    __storm_table__ = 'pieceinfo'
    id = storm.Int(primary=True)
    urlhash = storm.RawStr()
    peerid = storm.RawStr()
    #ipv4 = storm.RawStr()
    piecestart = storm.Int()
    pieceend = storm.Int()
    #关系
    rpeer = storm.Reference(peerid, DBPeer.peerid)
    rurl = storm.Reference(urlhash, DBUrl.urlhash)

    @staticmethod
    def sql_createtable():
        """ 创建表的sql语句 """
        return """
        CREATE TABLE `pieceinfo` (
  `id` int(11) NOT NULL auto_increment,
  `urlhash` char(20) NOT NULL,
  `peerid` char(20) NOT NULL,
  `piecestart` int(11) default NULL,
  `pieceend` int(11) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
        """

